<template>
	<view class="contain">
		<view class="fixed">
			<cu-custom :isBack="false" :color="color" bgColor="bg-shadeTop" @goBack="goBack">
				<block slot="content">
					<!--<view class="content">个人中心</view>-->
					<view class="content">{{lang.message.personalcenter}}</view>
				</block>
			</cu-custom>
		</view>
		<view class="list">
			<view class="list-box">
				<view class="item">
					<!--<view>头像</view>-->
					<view>{{lang.message.headprotrait}}</view>
					<view class="item-left" @tap="goImg">
						<image class="head" :src="head"></image>
						<image class="enter" src="../../static/images/enters.png"></image>
					</view>
				</view>
				<view class="item">
					<!--<view>昵称</view>-->
					<view>{{lang.message.name}}</view>
					<view class="item-left">
						<!--<input type="text" placeholder="请输入昵称" v-model="nickname">-->
						<input type="text" :placeholder="lang.message.inputname" v-model="nickname">
						<image class="enter" src="../../static/images/enters.png"></image>
					</view>
				</view>
				<picker @change="bindPickerChange" :value="index" :range="array">
					<view class="item">
						<!--<view>性别</view>-->
						<view>{{lang.message.gender}}</view>
						<view class="item-left">
							<view>{{sex}}</view>
							<image class="enter" src="../../static/images/enters.png"></image>
						</view>
					</view>
				</picker>
				<view class="item">
					<!--<view>年龄</view>-->
					<view>{{lang.message.age}}</view>
					<view class="item-left">
						<!--<input type="number" v-model="age" placeholder="请输入年龄">-->
						<input type="number" v-model="age" :placeholder="lang.message.inputage">
						<image class="enter" src="../../static/images/enters.png"></image>
					</view>
				</view>
				<view class="item">
					<!--<view>手机号</view>-->
					<view>{{lang.message.cellphonenumber}}</view>
					<view class="item-left">
						<view>{{phone}}</view>
						<image class="enter" src="../../static/images/enters.png"></image>
					</view>
				</view>
				<view class="item">
					<!--<view>签名</view>-->
					<view>{{lang.message.signature}}</view>
					<view class="item-left">
						<!--<input type="text" placeholder="请输入签名" v-model="write">-->
						<input type="text" :placeholder="lang.message.inputsignature" v-model="write">
						<image class="enter" src="../../static/images/enters.png"></image>
					</view>
				</view>
			</view>
			<!--<view class="btn1" @tap="goSubmit">提交</view>-->
			<view class="btn1" @tap="goSubmit">{{lang.message.commit}}</view>
		</view>
	</view>
</template>

<script>
	import {mapState, mapMutations} from "vuex";
	import Toast from '../../common/toast.js'
	export default {
		data() {
			return {
				color: '#333',
				head: '',
				nickname: '',
				sex: '男',
				//sex: this.lang.message.man,
				age: '18',
				phone: '',
				write: '',
				index: 0,
				array: ['男', '女'],
				type: 1
			}
		},
		computed: {
			...mapState(["lang"])
		},
		onLoad() {
			this.head = uni.getStorageSync('head')
			this.nickname = uni.getStorageSync('nickName')
			if(uni.getStorageSync('type') == 1) {
				//this.sex = '男'
				this.sex = this.lang.message.man
			} else {
				//this.sex = '女'
				this.sex = this.lang.message.woman
			}
			this.age = uni.getStorageSync('age')
			this.phone = uni.getStorageSync('phone')
			this.write = uni.getStorageSync('write')
			this.array[0] = this.lang.message.man
			this.array[1] = this.lang.message.woman
		},
		methods: {
			goBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.type = parseInt(this.index) + 1
				console.log(this.type)
				this.sex = this.array[this.index]
			},
			goImg() {
				let self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					success: function(res) {
						// 预览图片
						Toast.showLoading()
						res.tempFilePaths.forEach((item, index) => {
							uni.uploadFile({
								url: 'http://120.78.140.78:90/bm/authc/api/user/upload', //仅为示例，非真实的接口地址
								filePath: item,
								name: 'file',
								success: (uploadFileRes) => {
									Toast.hideLoading()
									let obj = JSON.parse(uploadFileRes.data)
									self.head = obj.data
									//Toast.showToast('修改成功')
									Toast.showToast(self.lang.message.modifysucc)
								},
								fail: (res) => {
									Toast.hideLoading()
									//Toast.showToast('上传失败')
									Toast.showToast(self.lang.message.uploadfail)
								}
							})
						})
					},
					fail: function(res) {
						Toast.hideLoading()
						//Toast.showToast('上传失败')
						Toast.showToast(self.lang.message.uploadfail)
					}
				})
			},
			goSubmit() {
				let data1 = {
					headImg: this.head,
					nickName: this.nickname,
					sex: this.type,
					age: parseInt(this.age),
					autograph: this.write
				}
				
				this.$u.post('authc/api/user/update', data1).then(res => {
					console.log(res)
					if (res.code == 0) {
						uni.setStorageSync('head', this.head)
						uni.setStorageSync('nickName', this.nickname)
						uni.setStorageSync('type', this.type.toString())
						uni.setStorageSync('age', this.age)
						uni.setStorageSync('write', this.write)
						//Toast.showToast('提交成功')
						Toast.showToast(this.lang.message.commitsucc)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						},500)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.contain {
		width: 100%;
		background: #F1F1F1;
		display: flex;
		flex-direction: column;
		align-items: center;

		.fixed {
			width: 100%;

			.content {
				width: 100%;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		.list {
			width: 100%;
			padding: 0 28rpx;
			box-sizing: border-box;
			margin-top: 30rpx;

			.btn1 {
				width: 80%;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 30rpx;
				color: #FFFFFF;
				margin: 120rpx auto 0;
				background: #005883;
				border-radius: 30px;
			}

			.list-box {
				width: 100%;
				border-radius: 6px;
				background: #FFFFFF;

				.item {
					width: 100%;
					padding: 28rpx;
					box-sizing: border-box;
					border-bottom: 1px solid #eee;
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 28rpx;
					color: #333;

					.item-left {
						display: flex;
						align-items: center;

						input {
							font-size: 28rpx;
							color: #333;
							text-align: right;
						}

						.head {
							width: 90rpx;
							height: 90rpx;
							border-radius: 50%;
						}

						.enter {
							width: 36rpx;
							height: 36rpx;
							margin-left: 30rpx;
						}
					}
				}
			}
		}
	}
</style>
